﻿using System;
using System.Web.UI.WebControls;
using FredCK.FCKeditorV2;
using Ue.Framework.Web.WebControls;
using Ue.Qcdz.Domain.SinglePages;
namespace Ue.Qcdz.UI.Controls
{
    public class SinglePageEdit : Ue.Infrastructure.Web.UI.SkinnedEntityEdit<SinglePage>
    {
        private TextBox tbxTitle;
        private FCKeditor tbxContent;
        private TextBox tbxImageUrl;
        private DropDownList ddlCategoryId;
        private TextBox tbxDisplayOrder;
        private RadioButtonList rBtnListStatus;

        public SinglePageEdit()
        {
            this.ReturnUrl = Domain.SiteUrls.Instance.SinglePageManage();
        }


        protected override void InitSkin(System.Web.UI.Control skin)
        {
            tbxTitle = skin.FindControl<TextBox>("tbxTitle");
            tbxContent = skin.FindControl<FCKeditor>("tbxContent");
            tbxImageUrl = skin.FindControl<TextBox>("tbxImageUrl");
            ddlCategoryId = skin.FindControl<DropDownList>("ddlCategoryId");
            tbxDisplayOrder = skin.FindControl<TextBox>("tbxDisplayOrder");
            rBtnListStatus = skin.FindControl<RadioButtonList>("rBtnListStatus");

            ddlCategoryId.Items.Clear();
            foreach (Ue.Qcdz.Domain.SinglePages.SinglePageType.SgPage pageType in (SinglePageType.SgPage[])Enum.GetValues(typeof(SinglePageType.SgPage)))
            {
                ListItem item = new ListItem();
                item.Text = pageType.ToString();
                item.Value = SinglePageType.GetSinglePageKey(pageType).ToString();
                ddlCategoryId.Items.Add(item);
            }
        }

        protected override string GetSkinFileName()
        {
            return "/Admin/Themes/Skins/SinglePages/SinglePageEdit.ascx";
        }


        protected override SinglePage Enttiy
        {
            get
            {
                SinglePage model = Domain.SinglePages.SinglePageService.GetSinglePage(this.EntityKey);
                if (model == null)
                    model = new SinglePage(this.EntityKey);
                if (tbxTitle != null)
                    model.Title = tbxTitle.GetValue<string>();
                if (tbxContent != null)
                    model.Content = tbxContent.Value;
                if (tbxImageUrl != null)
                    model.ImageUrl = tbxImageUrl.GetValue<string>();
                if (ddlCategoryId != null)
                    model.CategoryId = ddlCategoryId.SelectedValue;
                if (rBtnListStatus != null)
                    model.Status = rBtnListStatus.SelectedValue == "1" ? 1 : 0;
                if (tbxDisplayOrder != null)
                    model.DisplayOrder = tbxDisplayOrder.GetValue<int>();

                return model;
            }
            set
            {
                if (value == null)
                    return;

                tbxTitle.SetValue(value.Title);
                tbxContent.Value = value.Content;
                tbxImageUrl.SetValue(value.ImageUrl);
                tbxDisplayOrder.SetValue(value.DisplayOrder);

                if (ddlCategoryId != null)
                {
                    ddlCategoryId.SelectedIndex = -1;
                    if (ddlCategoryId.Items.FindByValue(value.CategoryId) != null)
                        ddlCategoryId.Items.FindByValue(value.CategoryId).Selected = true;
                }
                if (rBtnListStatus != null)
                {
                    rBtnListStatus.SelectedIndex = -1;
                    if (rBtnListStatus.Items.FindByValue(value.Status.ToString()) != null)
                        rBtnListStatus.Items.FindByValue(value.Status.ToString()).Selected = true;
                }

            }
        }
    }
}
